#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>
#define MSG_FILE "msgserver.c"
#define BUFFER 255
#define PERM S_IRUSR|S_IWUSR
struct msgtype{
	long mtype;
	char buffer[BUFFER+1];
};

int main(int argc, char *argv[])
{
	struct msgtype msg;
	key_t key;
	int msgid;
	if((key = ftok(MSG_FILE,'a')) == -1)
	{
		printf("Create Key Error:%s\n", strerror(errno));
		return -1;
	}
	if((msgid = msgget(key, PERM | IPC_CREAT | IPC_EXCL)) == -1)
	{
		printf("Create Message Error:%s\n", strerror(errno));
		return -1;
	}
	printf("msqid = %d\n",msgid);
	while(1)
	{
		memset(&msg, 0, sizeof(struct msgtype));
		msgrcv(msgid, &msg, sizeof(struct msgtype),1,0);
		printf("Server Received:%s\n", msg.buffer);
		msg.mtype = 2;
		msgsnd(msgid, &msg, sizeof(struct msgtype), 0);
	}
	return 0;
}
